package com.angel.provider.health.impl;


import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.angel.core.Constants;
import com.angel.core.util.CodeUtil;
import com.angel.core.util.Pager;
import com.angel.core.util.RemotePDFParsingUtils;
import com.angel.dao.health.BuzAssessmentMapper;
import com.angel.dao.health.BuzExaminationParsingMapper;
import com.angel.health.BuzAssessment;
import com.angel.health.BuzCustomer;
import com.angel.health.BuzExaminationParsingWithBLOBs;
import com.angel.health.PDFEntity;
import com.angel.provider.health.BuzCustomerProvider;
import com.angel.provider.health.BuzExaminationParsingProvider;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@Service
public class BuzExaminationParsingProviderImpl implements BuzExaminationParsingProvider{

//	@Autowired
//	private MongoTemplate template;
	@Autowired
	private BuzExaminationParsingMapper mapper;
	@Autowired
	private BuzAssessmentMapper baMapper;
	@Autowired
	private BuzCustomerProvider service;
	
	public void save(String path,Integer examinationId) {
		try {
			PDFEntity entity = RemotePDFParsingUtils.ParsingPDFToObject(path);
			BuzCustomer customer = entity.getCustomer();
			List<Map<String, Object>> cusList = service.findCustomerByPhone(customer);
			if(cusList.size() > 0){
				customer.setId(Integer.parseInt(cusList.get(0).get("id").toString()));
				service.updateById(customer);
			}else{
				customer.setLabelId(1);
				customer.setMemberForm(0);
				service.insert(customer);
				
				customer.setCode(CodeUtil.createCode(null, Constants.CUSTOMERNUM, customer.getId()));
				service.updateById(customer);
			}
			BuzExaminationParsingWithBLOBs blobs = entity.getBlobs();
			blobs.setCustomerId(customer.getId());
			blobs.setExaminationId(examinationId);
			mapper.insert(blobs);
			
			//解析完成后在buz_assessment中更新数据
			BuzAssessment ba = new BuzAssessment();
			ba.setCustomerId(customer.getId());
			ba.setExaminationParsingId(blobs.getId());
			Date date = new Date();
			ba.setUpdateTime(date);
			ba.setCreateTime(date);
			ba.setEnable(1);
			baMapper.insert(ba);
			ba.setCode(CodeUtil.createCode(null, Constants.ASSESSMENT, ba.getId()));
			baMapper.updateByPrimaryKeySelective(ba);
//			List<Map<String, Object>> baList = baMapper.findAssessmentAll(ba);
//			Date date = new Date();
//			if(baList.size() > 0){
//				ba.setExaminationParsingId(blobs.getId());
//				ba.setUpdateTime(date);
//				ba.setId(Integer.parseInt(baList.get(0).get("id").toString()));
//				baMapper.updateByPrimaryKeySelective(ba);
//			}else{
//				ba.setExaminationParsingId(blobs.getId());
//				ba.setUpdateTime(date);
//				ba.setCreateTime(date);
//				ba.setEnable(1);
//				baMapper.insert(ba);
//				
//				BuzAssessment e = new BuzAssessment();
//				e.setCode(CodeUtil.createCode(null, Constants.ASSESSMENT, ba.getId()));
//				e.setId(ba.getId());
//				baMapper.updateByPrimaryKeySelective(e);
//			}
//			template.save(blobs);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

//	public List<BuzExaminationParsingWithBLOBs> findAll(
//			BuzExaminationParsingWithBLOBs parsing) {
//		Query query = new Query();
//		if(parsing != null){
//			if(parsing.getCustomerId() != null){
//				query.addCriteria(Criteria.where("customerId").is(parsing.getCustomerId()));
//			}
//			if(parsing.getExaminationId() != null){
//				query.addCriteria(Criteria.where("examinationId").is(parsing.getExaminationId()));
//			}
//			if(parsing.getInformation() != null && parsing.getInformation() != ""){
//				query.addCriteria(Criteria.where("information").is(parsing.getInformation()));
//			}
//			if(parsing.getMedical() != null && parsing.getMedical() != ""){
//				query.addCriteria(Criteria.where("medical").is(parsing.getMedical()));
//			}
//			if(parsing.getSummary() != null && parsing.getSummary() != ""){
//				query.addCriteria(Criteria.where("summary").is(parsing.getSummary()));
//			}
//		}
//		List<BuzExaminationParsingWithBLOBs> list = template.find(query , BuzExaminationParsingWithBLOBs.class);
//		return list;
//	}
	
	public List<BuzExaminationParsingWithBLOBs> findParsing(BuzExaminationParsingWithBLOBs parsing){
		return mapper.findParsing(parsing);
	}

//	public BuzExaminationParsingWithBLOBs findById(String id) {
//		return template.findById(id, BuzExaminationParsingWithBLOBs.class);
//	}

//	public void delete(BuzExaminationParsingWithBLOBs parsing) {
//		Query query = new Query();
//		query.addCriteria(Criteria.where("_id").is(parsing.getId()));
//		template.remove(query , BuzExaminationParsingWithBLOBs.class);
//	}

//	public void deleteAll() {
//		Query query = new Query();
//		template.remove(query , BuzExaminationParsingWithBLOBs.class);
//	}

//	public void update(BuzExaminationParsingWithBLOBs parsing) {
//		Query query = new Query();
//		query.addCriteria(Criteria.where("_id").is(parsing.getId()));
//		Update update = new Update();
//		update.set("customerId", parsing.getCustomerId());
//		update.set("examinationId", parsing.getExaminationId());
//		update.set("information", parsing.getInformation());
//		update.set("medical", parsing.getMedical());
//		update.set("summary", parsing.getSummary());
//		template.updateFirst(query, update, BuzExaminationParsingWithBLOBs.class);
//	}

	/* (non-Javadoc)
	 * @see com.angel.provider.health.BuzExaminationParsingProvider#findParsingAll(com.angel.health.BuzExaminationParsingWithBLOBs)
	 */
	@Override
	public List<BuzExaminationParsingWithBLOBs> findParsingAll(BuzExaminationParsingWithBLOBs parsing) {
		// TODO Auto-generated method stub
		return mapper.findParsing(parsing);
	}

	@Override
	public void insert(BuzExaminationParsingWithBLOBs parsing) {
		// TODO Auto-generated method stub
		mapper.insert(parsing);
	}

	@Override
	public BuzExaminationParsingWithBLOBs findById(BuzExaminationParsingWithBLOBs parsing) {
		// TODO Auto-generated method stub
		int id = parsing.getId();
		return mapper.selectByPrimaryKey(id);
	}

	/* (non-Javadoc)
	 * @see com.angel.provider.health.BuzExaminationParsingProvider#findAll(com.angel.health.BuzExaminationParsingWithBLOBs)
	 */
	@Override
	public Pager findAllForPage(BuzExaminationParsingWithBLOBs parsing,Pager pager) {
		// TODO Auto-generated method stub
		PageHelper.startPage(pager.getCurrentPage(), 1);
		List<Map<String, Object>> list = mapper.findAll(parsing);
		PageInfo<Map<String, Object>> pageInfo = new PageInfo<Map<String, Object>>(list);
		pager.setTotal(pageInfo.getTotal());
		pager.setTotalPages(pageInfo.getPages());
		pager.setList(pageInfo.getList());
		return pager;
	}

}
